package com.haochang.chunkvod.app.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

/**
 * @author Yangjie
 *         className RequestPermissionsUtil Android6.0请求权限工具类
 *         created at  2016/9/26  10:18
 */
public class RequestPermissionsUtil {

    static RequestPermissionsUtil instance;
    public static int PERMISSION_LOCATION = 1;
    public static int PERMISSION_CAMERA = 2;
    public static int PERMISSION_READ_EXTERNAL_STORAGE = 3;
    public static int PERMISSION_WRITE_EXTERNAL_STORAGE = 4;
    public static int PERMISSION_WRITE_READ_EXTERNAL_STORAGE = 5;

    public static RequestPermissionsUtil getInstance() {
        if (null == instance) {
            synchronized (RequestPermissionsUtil.class) {
                if (null == instance) {
                    instance = new RequestPermissionsUtil();
                }
            }
        }
        return instance;
    }

    /**
     * @param activity
     * @param permissions new String{ Manifest.permission.ACCESS_COARSE_LOCATION }
     * @return true 需要申请权限，false已有权限
     */
    public boolean checkPermissions(Activity activity, String[] permissions, int requestCode) {
        if (null != permissions) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(activity, permissions, requestCode);
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * @param activity
     * @param permissions new String{ Manifest.permission.ACCESS_COARSE_LOCATION }
     */
//    private void checkPermissions(Activity activity, String[] permissions, int requestCode) {
//        if (null != permissions) {
//            List<String> listPermission = new ArrayList<String>();
//            String[] permissionsTemp;
//            for (String permission : permissions) {
//                if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
//                    listPermission.add(permission);
//                }
//            }
//            if (listPermission.size() > 0) {
//                permissionsTemp = (String[]) listPermission.toArray();
//                ActivityCompat.requestPermissions(activity, permissionsTemp, requestCode);
//            }
//        }
//    }
}
